package com.jddup.program.etm.sqlserver;

import com.jddup.common.annotations.ColumnDes;
import com.jddup.common.pojo.TableColumn;
import com.jddup.program.etm.TableColumnColumnDesConvertor;
import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;

/**
 * Created by ZZB on 2017/12/4.
 */
public class SqlserverTableColumnColumnDesConvertor implements TableColumnColumnDesConvertor {
    @Override
    public void convertFromColumnDes(String tableName ,TableColumn tableColumn, ColumnDes columnDes) {
        Class clazz = tableColumn.getPropertyType();
        if(clazz.equals(String.class)){
            if(columnDes.maxLength() <= 255 && columnDes.maxLength() != 0) {
                tableColumn.setColumnType("VARCHAR");
            } else if (columnDes.maxLength()> 65535L && columnDes.maxLength() <= 16777215L){
                tableColumn.setColumnType("MEDIUMTEXT");
                tableColumn.setMaxLength(16777215L);
            } else if (columnDes.maxLength()> 16777215L ){
                tableColumn.setColumnType("LONGTEXT");
                tableColumn.setMaxLength(16777215L);
            } else {
                tableColumn.setColumnType("TEXT");
                tableColumn.setMaxLength(65535L);
            }
        } else if (clazz.equals(Integer.class) || clazz.equals(int.class)){
            if(StringUtils.isBlank(columnDes.maximum())){
                tableColumn.setColumnType("INT");
            } else {
                BigInteger bigInteger = new BigInteger(columnDes.maximum());
                if (columnDes.signed()) {
                    if (bigInteger.compareTo(BigInteger.valueOf(127)) <= 0) {
                        tableColumn.setColumnType("TINYINT");
                    } else if (bigInteger.compareTo(BigInteger.valueOf(32767)) <= 0) {
                        tableColumn.setColumnType("SMALLINT");
                    } else if (bigInteger.compareTo(BigInteger.valueOf(8388607)) <= 0) {
                        tableColumn.setColumnType("MEDIUMINT");
                    } else {
                        tableColumn.setColumnType("INT");
                    }
                } else {
                    if (bigInteger.compareTo(BigInteger.valueOf(255)) <= 0) {
                        tableColumn.setColumnType("TINYINT");
                    } else if (bigInteger.compareTo(BigInteger.valueOf(65535)) <= 0) {
                        tableColumn.setColumnType("SMALLINT");
                    } else if (bigInteger.compareTo(BigInteger.valueOf(16777215)) <= 0) {
                        tableColumn.setColumnType("MEDIUMINT");
                    } else {
                        tableColumn.setColumnType("INT");
                    }
                }
            }
        } else if(clazz.equals(Byte[].class) || clazz.equals(byte[].class)){
            if(columnDes.maxLength() <= 255 && columnDes.maxLength() != 0) {
                tableColumn.setColumnType("VARBINARY");
            } else if (columnDes.maxLength()> 65535L && columnDes.maxLength() <= 16777215L){
                tableColumn.setColumnType("MEDIUMBLOB");
                tableColumn.setMaxLength(16777215L);
            } else if (columnDes.maxLength()> 16777215L ){
                tableColumn.setColumnType("LONGBLOB");
                tableColumn.setMaxLength(16777215L);
            } else {
                tableColumn.setColumnType("BLOB");
                tableColumn.setMaxLength(65535L);
            }
        }
        setCommonPropertiesInTableColumn(tableColumn,columnDes);
    }
}
